package com.tc.multicast;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class Multicast {
	static {
		System.loadLibrary("hello-jni");
	}
	String senderChannel;
	String receiverChannel;
	short senderPort;
	short receiverPort;
	String localIp;
	short localPort;
	public Multicast(String senderChannel, short senderPort, String receiverChannel,
			 short receiverPort, String localIp,
			short localPort) {
		super();
		this.senderChannel = senderChannel;
		this.receiverChannel = receiverChannel;
		this.senderPort = senderPort;
		this.receiverPort = receiverPort;

			
			 Enumeration<NetworkInterface> netInterfaces = null;
		        StringBuffer sb = new StringBuffer("");
		        try {
		            netInterfaces = NetworkInterface.getNetworkInterfaces();
		            while (netInterfaces.hasMoreElements()) {
		                NetworkInterface ni = netInterfaces.nextElement();
		                Enumeration<InetAddress> ips = ni.getInetAddresses();
		                while (ips.hasMoreElements()) {
		                 InetAddress local = ips.nextElement();
		                 if(!local.isLoopbackAddress())
		                	 this.localIp = local.getHostAddress();
		                }
		            }
		        } catch (Exception e) {
		            e.printStackTrace();
		        }


		
		this.localPort = localPort;
		try {
			init(senderChannel,senderPort,receiverChannel,receiverPort,this.localIp,localPort);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	native void init(String senderIp, short senderPort,String receiverIp, short receiverPort,String localIp,short localPort)throws Exception;
	public native void send(byte[] data)throws Exception;
	public native byte[] receive()throws Exception;
	public native byte[] close();
	
}
